package com.gmss.crawler.service;

import com.alibaba.fastjson.JSON;
import com.gmss.crawler.constants.Constants;
import com.gmss.crawler.entity.AcademicianEntity;
import com.gmss.crawler.enums.AcademicianEnum;
import com.gmss.crawler.enums.CompanyEnum;
import com.gmss.crawler.module.person.domain.PersonInfo;
import com.gmss.crawler.module.test.domain.PersonInfoTopy;
import com.gmss.crawler.utils.CommonUtils;
import com.gmss.crawler.utils.CrawlerCommonUtils;
import com.gmss.crawler.utils.FileUtil;
import com.gmss.crawler.utils.RegexUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

/**
 * 中国科学院院士爬取
 */
@Slf4j
@Service
public class CasCrawlerService {

    @Value("img_file_path")
    private String imgFilePath;

    // 中国科学院院士-体院士名单（不包含外籍院士名单与简介、已故院士名单、已故外籍院士名单）
    public static String index = "http://casad.cas.cn/ysxx2017/ysmdyjj/qtysmd_124280/";

    public static void main(String[] args) throws IOException {

        List<PersonInfo> personInfoList = new CasCrawlerService().addData1();
        personInfoList.addAll(new CasCrawlerService().addData());
        List<String> nameList = personInfoList.stream().map(PersonInfo::getName).collect(Collectors.toList());
        System.out.println(nameList.size());
        System.out.println(JSON.toJSONString(nameList));
        //crawlerCas();

//        孙昌璞
//        Map<String, AcademicianEntity> academicianEntityMap = new HashMap<>();
//        AcademicianEntity academicianEntity = new AcademicianEntity().builder().name("王恩哥").build();
//        academicianEntityMap.put("王恩哥", academicianEntity);
//        academicianEntity = new AcademicianEntity().builder().name("艾伦·黑格").build();
//        academicianEntityMap.put("艾伦·黑格", academicianEntity);
//        academicianEntity = new AcademicianEntity().builder().name("王广厚").build();
//        academicianEntityMap.put("王广厚", academicianEntity);
//        // 2011
//        academicianEntity = new AcademicianEntity().builder().name("罗格？欧文").build();
//        academicianEntityMap.put("罗格？欧文", academicianEntity);
//        //
//        academicianEntity = new AcademicianEntity().builder().name("克里斯汀•阿芒托").build();
//        academicianEntityMap.put("克里斯汀•阿芒托", academicianEntity);
//        // 2013
//        academicianEntity = new AcademicianEntity().builder().name("苏布拉•苏雷什").build();
//        academicianEntityMap.put("苏布拉•苏雷什", academicianEntity);
//        //
//        academicianEntity = new AcademicianEntity().builder().name("黄如").build();
//        academicianEntityMap.put("黄如", academicianEntity);
//        //  2015
//        academicianEntity = new AcademicianEntity().builder().name("马丁·格勒切尔").build();
//        academicianEntityMap.put("马丁·格勒切尔", academicianEntity);
//        academicianEntity = new AcademicianEntity().builder().name("默罕默德·贾马·迪恩").build();
//        academicianEntityMap.put("默罕默德·贾马·迪恩", academicianEntity);
//        academicianEntity = new AcademicianEntity().builder().name("陈祖煜").build();
//        academicianEntityMap.put("陈祖煜", academicianEntity);
//
////        String url = "http://casad.cas.cn/yszx2017/lcdxysmd/201504/t20150417_4681087.html";
//        Map<String, String> fileLinesMap = FileUtil.readFileLines("/crawlerUrlFile/cas.txt", ";");
//        // 这里的院士类型是错误的，只是测试使用
//        parseWordData(fileLinesMap, academicianEntityMap, 1);

//        List<AcademicianEntity> list = new CasCrawlerService().crawlerCas();


    }


    public List<AcademicianEntity> crawlerCas() {
        imgFilePath = imgFilePath + File.separator;
        Document doc1 = CrawlerCommonUtils.getDocument(index);
        Map mapUrl = new HashMap();
        mapUrl.put("全体院士", index);
        List<Map> allUrlList = new ArrayList<>();
        // 获取院士列表
        Elements select = doc1.select(".intr li");
        for (int i = 0; i < select.size(); i++) {
            Element element = select.get(i);
            if (i == 1 || i == 2 || i == 3) {
                mapUrl.put(element.select("a").text(), element.select("a").attr("href"));
            }
        }
        // 爬取的url,全体院士名单、外籍院士名单与简介、已故院士名单、已故外籍院士名单
        allUrlList.add(mapUrl);
        Map<String, AcademicianEntity> allMap = new HashMap<>();
        AtomicReference<Map<String, AcademicianEntity>> academicianEntityMap = new AtomicReference<>();
        AtomicReference<Integer> type = new AtomicReference<>(-1);
        for (Map map1 : allUrlList) {
            map1.forEach((key, value) -> {
                //解析获取到的每个url地址
                Document doc = CrawlerCommonUtils.getDocument((String) value);
                Elements partElements = doc.getElementById("allNameBar").select("dt");
                Elements nameElements = doc.getElementById("allNameBar").select("dd");
                //截取list起始位置
                int i = 0;
                //把名字和URL地址存放到map中，然后把map存放到list中
                List<Map<String, String>> list = new ArrayList<>();
                //key--部门名称  value--存放名字和URL地址的list集合
                for (Element partElement : partElements) {
                    String partContent = partElement.text();
                    // 获取每个学部的人数
                    String people = partContent.substring(partContent.replaceAll("（", "(").indexOf("(") + 1, partContent.indexOf("人"));
                    for (Element element : nameElements) {
                        Elements spanElements = element.select("span");
                        // 获取每个院士及对应个人简介的URL
                        for (Element spanElement : spanElements) {
                            Map<String, String> nameAndURLmap = new HashMap<>();
                            String aUrl = spanElement.select("a").attr("href");
                            String aName = spanElement.select("a").text();
                            nameAndURLmap.put(aName, aUrl);
                            list.add(nameAndURLmap);
                        }
                    }
                    //截取每个部的院士
                    List<Map<String, String>> list2 = list.subList(i, Integer.parseInt(people) + i);
//                    log.debug("------{}----:{}", people, JSON.toJSONString(list2));
                    //下一次截取list的位置
                    i += Integer.parseInt(people);
                    //把部门和截取到每个部的院士添加到map,Map<部门，List<院士姓名，院士姓名URL>>
                    Map<String, List<Map<String, String>>> map = new HashMap<>();
                    map.put(partContent, list2);
                    key.toString().replaceAll("一、", "").replaceAll("二、", "").replaceAll("三、", "")
                            .replaceAll("四、", "").replaceAll("五、", "").replaceAll("六、", "")
                            .replaceAll("七、", "").replaceAll("八、", "").replaceAll("九、", "")
                            .replaceAll("（", "(").replaceAll("）", ")");
                    key = RegexUtils.replaceData(key.toString(), RegexUtils.ALL_KUA_REGEX, "");
                    // 根据 全体院士名单、外籍院士名单与简介、已故院士名单、已故外籍院士名单 获取对应的类型
                    type.set(getAcademicianType("中国科学院-" + key.toString()));
                    //解析数据，获取图片
                    academicianEntityMap.set(parseData(map, type.get()));
                    log.info("中国科学院-{}:{}", key, academicianEntityMap.get().size());
                    allMap.putAll(academicianEntityMap.get());
                }
            });
        }
        try {
            Map<String, String> fileLinesMap = FileUtil.readFileLines("/crawlerUrlFile/cas.txt", ";");

            parseWordData(fileLinesMap, allMap, type.get());
        } catch (IOException e) {
            log.error("Exception: {}", e);
        }
        List<AcademicianEntity> list = new ArrayList<>();

        if (MapUtils.isNotEmpty(allMap)) {
            allMap.forEach((key, value) -> {
                list.add(value);
            });
        }

        return list;

    }

    public Map<String, AcademicianEntity> parseData(Map<String, List<Map<String, String>>> map, Integer academicianType) {
        Map<String, AcademicianEntity> academicianEntityMap = new HashMap<>();
        map.forEach((key, value) -> {
            for (Map<String, String> stringStringMap : value) {
                stringStringMap.forEach((name, url) -> {
                    name = CommonUtils.handleForeignName(name).replaceAll("（", "(").replaceAll("）", ")");
                    name = RegexUtils.replaceData(name, RegexUtils.ALL_KUA_REGEX, "");
                    name = RegexUtils.replaceData(name, RegexUtils.CHINA, "");
//                    log.debug("院士姓名：{}", name);
                    Document doc = CrawlerCommonUtils.getDocument(url);
                    //获取个人简介
                    String describe = doc.getElementById("zoom").select("div").select("p").text();
                    //获取图片地址
                    //截取个人详情页地址
                    String str = url.substring(0, url.lastIndexOf("/"));
                    String iURL = doc.getElementById("zoom").select(".acadImg").select("img").attr("src");
                    String fileName = "";
                    if (StringUtils.isNotBlank(iURL)) {
                        String imgURL = str + iURL.substring(1, iURL.length());
                        //获取图片后缀
                        String postName = imgURL.substring(imgURL.lastIndexOf("."));
                        //重命名图片
                        name = CommonUtils.handleForeignName(name);
                        fileName = name + postName;
                        //保存图片
                        //文件路径
                        File file = new File(imgFilePath + fileName);
                        if (!file.exists()) {
                            imgURL = imgURL.contains(" ") ? imgURL.replaceAll(" ", "%20") : imgURL;
                            CrawlerCommonUtils.getPersonalImgSave(imgURL, imgFilePath, fileName);
                        }

                    } else {
//                        log.info("没有个人图片");
                    }

                    String key2 = key.replaceAll("一、", "").replaceAll("二、", "").replaceAll("三、", "")
                            .replaceAll("四、", "").replaceAll("五、", "").replaceAll("六、", "")
                            .replaceAll("七、", "").replaceAll("八、", "").replaceAll("九、", "")
                            .replaceAll("（", "(").replaceAll("）", ")");
                    key2 = RegexUtils.replaceData(key2, RegexUtils.ALL_KUA_REGEX, "");
                    if (StringUtils.equalsAny(key2, "已故外籍院士名单", "外籍院士名单与简介")) {
                        key2 = "";
                    }

                    String sex = null;
                    if (StringUtils.contains(describe.replaceAll(" ", "").replaceAll("，", ","), ",男,")) {
                        sex = "男";
                    } else if (StringUtils.contains(describe.replaceAll(" ", "").replaceAll("，", ","), ",女,")) {
                        sex = "女";
                    }

                    //获取院士姓名、个人图像、部门、院士头街、个人简介、性别
                    AcademicianEntity academicianEntity = new AcademicianEntity().builder()
                            .name(name)
                            .imgName(fileName)
                            .department(key2)
                            // 院士头街
                            .academicianTitle(academicianType)
                            .academicianTitleValue(AcademicianEnum.getEnum(academicianType).getValue())
                            .describeData(describe)
                            .sex(sex)
                            .build();
                    academicianEntityMap.put(name, academicianEntity);
                });
            }
        });
        return academicianEntityMap;
    }

    //根据院士头衔获取对应的code
    public static Integer getAcademicianType(String key) {
        Integer type = -1;
        switch (key) {
            case "中国科学院-全体院士":
                type = AcademicianEnum.CAS_ACADEMICIAN.getCode();
                break;
            case "中国科学院-外籍院士名单与简介":
                type = AcademicianEnum.CAS_FOREIGN_ACADEMICIAN.getCode();
                break;
            case "中国科学院-已故院士名单":
                type = AcademicianEnum.CAS_DIE_ACADEMICIAN.getCode();
                break;
            case "中国科学院-已故外籍院士":
                type = AcademicianEnum.CAS_DIE_FOREIGN_ACADEMICIAN.getCode();
                break;
            case "中国工程院-全体院士":
                type = AcademicianEnum.CAE_ACADEMICIAN.getCode();
                break;
            case "中国工程院-已故院士名单":
                type = AcademicianEnum.CAE_DIE_ACADEMICIAN.getCode();
                break;
            case "中国工程院-外籍院士名单":
                type = AcademicianEnum.CAE_FOREIGN_ACADEMICIAN.getCode();
                break;
            case "中国工程院-已故外籍院士名单":
                type = AcademicianEnum.CAE_DIE_FOREIGN_ACADEMICIAN.getCode();
                break;
        }
        return type;
    }

    /**
     * 获取专业和工作单位
     *
     * @throws IOException
     */
    public static void parseWordData(Map<String, String> fileLinesMap, Map<String, AcademicianEntity> academicianEntityMap, Integer type) throws IOException {
//        fileLinesMap.forEach((key, value) -> {
        //遍历fileLinesMap
        for (Map.Entry<String, String> entry : fileLinesMap.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();
//            key = "2007";
//            value = "http://casad.cas.cn/yszx2017/lcdxysmd/201504/t20150417_4681086.html";
            if (StringUtils.contains(value, "http")) {
                Document doc = CrawlerCommonUtils.getDocument(value);
                // 验证通过-2001、
                if (StringUtils.containsAny(key, "2001", "2005")) {
                    Elements tbodyEle = doc.select("#artibody table tbody");
                    getTableData(academicianEntityMap, tbodyEle, type);
                }
                // 验证通过-2007
                if (StringUtils.contains(key, "2007")) {
                    Elements tbodyEle = doc.select("#artibody table tbody");
                    Map<String, Integer> tbodyMap = new HashMap<>();
                    for (int i = 0; i < tbodyEle.size(); i++) {
                        Elements trElements = tbodyEle.get(i).select("tr");
                        if (trElements.size() == 0) {
                            tbodyEle = doc.select("#artibody table thead");
                            getTableData(academicianEntityMap, tbodyEle, type);
                            continue;
                        }
                        Elements thead = trElements.get(0).select("td");
                        if (thead.size() < 1) {
                            thead = trElements.get(0).select("th");
                        }
                        tbodyMap = getStringIntegerMap(thead);
                        setAcademicianEntityMap(academicianEntityMap, tbodyMap, trElements, type);
                    }
                }
                // 验证通过-2003、
                if (StringUtils.contains(key, "2003")) {
                    Elements tbodyEle = doc.select("table tbody");
                    Map<String, Integer> tbodyMap = new HashMap<>();
                    for (int i = 0; i < tbodyEle.size(); i++) {
                        Elements trElements = tbodyEle.get(i).select("tr");
                        for (int j = 0; j < trElements.size(); j++) {
                            Elements thead = trElements.get(j).select("td");
                            if (thead.size() <= 1) {
                                continue;
                            }
                            tbodyMap = getStringIntegerMap(thead);
                            setAcademicianEntityMap(academicianEntityMap, tbodyMap, trElements, type);
                        }
                    }
                }
                // 验证通过-2009、2011、2013、2015、2019
                if (StringUtils.containsAny(key, "2009", "2011", "2013", "2015", "2019")) {
                    Elements select = doc.select("#zoom table");
                    for (int i = 0; i < select.size(); i++) {
                        Elements thead = select.get(i).select("thead tr th");
                        if (thead.size() == 0) {
                            thead = select.get(i).select("thead tr td");
                        }
                        if (thead.size() == 0) {
                            thead = select.get(i).select("tbody tr th");
                        }
                        if (thead.size() == 0) {
                            thead = select.get(i).select("tbody tr td");
                        }
                        Map<String, Integer> tbodyMap = getStringIntegerMap(thead);
                        Elements trElements = select.get(i).select("tbody").select("tr");
                        setAcademicianEntityMap(academicianEntityMap, tbodyMap, trElements, type);
                    }
                }
                // 验证通过
                if (StringUtils.contains(key, "2017")) {
                    //2017中国科学院增选院士.html
                    doc = CrawlerCommonUtils.getDocumentHtml("/htmlTest/2017中国科学院增选院士.html");
                    Elements select = doc.select("table tbody");
                    getTableData(academicianEntityMap, select, type);
                }
            }
        }
//        });
    }


    public static void getNameListData(Map<String, AcademicianEntity> map, Elements tbodyEle, String departmentText, String year) {
        Map<String, Integer> tbodyMap = new HashMap<>();
        for (int i = 0; i < tbodyEle.size(); i++) {
            Elements trElements = tbodyEle.get(i).select("tr");
            Elements thead = trElements.get(0).select("td");
            if (thead.size() < 1) {
                thead = trElements.get(0).select("th");
            }
            tbodyMap = getStringIntegerMap(thead);
            getNameListDataMap(map, tbodyMap, trElements, departmentText, year);
        }
    }

    public static void getNameListDataMap(Map<String, AcademicianEntity> map, Map<String, Integer> tbodyMap, Elements trElements, String departmentText, String year) {
        for (int j = 0; j < trElements.size(); j++) {
            //姓名
            int finalJ = j;
            String name = "";
            AcademicianEntity entity = new AcademicianEntity();
            for (Map.Entry<String, Integer> entry : tbodyMap.entrySet()) {
                String key = entry.getKey();
                Integer value = entry.getValue();
                Elements tdElements = trElements.get(finalJ).select("td");
                if (!(tdElements.size() > 0) || tdElements.size() == 1) {
                    continue;
                }

                if (StringUtils.equalsAny(key, "姓名", "中文姓名")) {
                    name = CommonUtils.replaceBlank(tdElements.get(value).text()).replaceAll("  ", "").replaceAll(" ", "").replaceAll("（", "(").replaceAll("）", ")");
                    name = RegexUtils.replaceData(name, RegexUtils.ALL_KUA_REGEX, "");
                    name = RegexUtils.replaceData(name, RegexUtils.CHINA, "");
                    entity.setName(name);
//                    map.put(name, departmentText);
//                    list.add(name);
                }
                if (StringUtils.isNotBlank(departmentText)) {
                    entity.setDepartment(departmentText);
                }
                if (StringUtils.isNotBlank(year)) {
                    entity.setElectionYear(year);
                    entity.setTitleYears(0);
                }
                if (StringUtils.isNotBlank(entity.getName())) {
                    map.put(name, entity);
                }
            }
        }
    }

    /**
     * 获取table数据
     *
     * @param academicianEntityMap
     * @param tbodyEle
     */
    public static void getTableData(Map<String, AcademicianEntity> academicianEntityMap, Elements tbodyEle, Integer type) {
        Map<String, Integer> tbodyMap = new HashMap<>();
        for (int i = 0; i < tbodyEle.size(); i++) {
            Elements trElements = tbodyEle.get(i).select("tr");
            Elements thead = trElements.get(0).select("td");
            if (thead.size() < 1) {
                thead = trElements.get(0).select("th");
            }
            tbodyMap = getStringIntegerMap(thead);
            setAcademicianEntityMap(academicianEntityMap, tbodyMap, trElements, type);
        }
    }

    /**
     * @param academicianEntityMap 院士map
     * @param tbodyMap             院士table表头
     * @param trElements           院士table内容
     * @param type                 院士头衔
     * @return
     */
    public static Map<String, AcademicianEntity> setAcademicianEntityMap(Map<String, AcademicianEntity> academicianEntityMap, Map<String, Integer> tbodyMap, Elements trElements, Integer type) {
        for (int j = 0; j < trElements.size(); j++) {
            //姓名
            int finalJ = j;
            String data = "";
            String name = "";
//            AcademicianEntity academicianEntity = new AcademicianEntity();
            for (Map.Entry<String, Integer> entry : tbodyMap.entrySet()) {
                String key = entry.getKey();
                Integer value = entry.getValue();
                Elements tdElements = trElements.get(finalJ).select("td");
                if (!(tdElements.size() > 0) || tdElements.size() == 1) {
                    continue;
                }

                if (StringUtils.equalsAny(key, "姓名", "中文姓名")) {
                    name = tdElements.get(value).text().replaceAll(" ", "").replaceAll("（", "(").replaceAll("）", ")");

                    if (name.contains("女")) {
                        name = RegexUtils.replaceData(name, RegexUtils.ALL_KUA_REGEX, "");
                        name = CommonUtils.handleForeignName(name);
                        if (academicianEntityMap.containsKey(name)) {
                            academicianEntityMap.get(name).setSex("女");
                        } else {
                            name = RegexUtils.replaceData(name, RegexUtils.CHINA, "");
//                            academicianEntityMap.put(name, academicianEntity.builder().name(name).sex("女").build());
                        }
                    } else {
                        name = RegexUtils.replaceData(name, RegexUtils.CHINA, "");
                        academicianEntityMap.get(name).setSex("男");
//                        academicianEntityMap.put(name, academicianEntity.builder().name(name).sex("男").build());
                    }
                }
                if (StringUtils.equalsAny(key, "英文姓名")) {
                    data = trElements.get(finalJ).select("td").get(value).text().replaceAll(" ", "").replaceAll("\\?", "•");
                    if (academicianEntityMap.containsKey(name)) {
                        academicianEntityMap.get(name).setForeignName(data);
                    }
                }
                if (StringUtils.equalsAny(key, "出生年月", "出生日期")) {
                    data = trElements.get(finalJ).select("td").get(value).text().replaceAll(" ", "");
                    if (academicianEntityMap.containsKey(name)) {
                        academicianEntityMap.get(name).setBirthday(data);
                    } else {
//                        academicianEntityMap.put(name, academicianEntity.builder().name(name).birthday(data).build());
                    }
                }
                if (StringUtils.equalsAny(key, "逝世日期")) {
                    data = trElements.get(finalJ).select("td").get(value).text().replaceAll(" ", "");
                    if (academicianEntityMap.containsKey(name)) {
                        academicianEntityMap.get(name).setDateOfDeath(data);
                    } else {
//                        academicianEntityMap.put(name, academicianEntity.builder().name(name).dateOfDeath(data).build());
                    }
                }
                if (StringUtils.equalsAny(key, "生卒年月")) {
                    data = trElements.get(finalJ).select("td").get(value).text().replaceAll(" ", "").replaceAll("-", ".");
                    if (academicianEntityMap.containsKey(name)) {
                        academicianEntityMap.get(name).setBirthday(data.substring(0, 10));
                        academicianEntityMap.get(name).setDateOfDeath(data.substring(11));
                    } else {
//                        academicianEntityMap.put(name, academicianEntity.builder().name(name).birthday(data.substring(0,10)).dateOfDeath(data.substring(11)).build());
                    }
                }
                if (StringUtils.equalsAny(key, "专业", "学科领域", "学科专业")) {
                    data = trElements.get(finalJ).select("td").get(value).text().replaceAll(" ", "");
                    if (academicianEntityMap.containsKey(name)) {
                        academicianEntityMap.get(name).setMajor(data);
                    } else {
//                        academicianEntityMap.put(name, academicianEntity.builder().name(name).major(data).build());
                    }
                }
                if (StringUtils.equalsAny(key, "国籍")) {
                    data = trElements.get(finalJ).select("td").get(value).text().replaceAll(" ", "");
                    if (academicianEntityMap.containsKey(name)) {
                        academicianEntityMap.get(name).setNationality(data);
                    } else {
//                        academicianEntityMap.put(name, academicianEntity.builder().name(name).nationality(data).build());
                    }
                }
                if (StringUtils.equalsAny(key, "工作单位", "所在单位")) {
                    if (academicianEntityMap.containsKey(name)) {
                        data = trElements.get(finalJ).select("td").get(value).text().replaceAll(" ", "");
                        data = data.replaceAll("（", "(").replaceAll("）", ")");
                        academicianEntityMap.get(name).setWorkCompany(data);
                        if (StringUtils.isNotBlank(academicianEntityMap.get(name).getNationality()) && !StringUtils.equals(academicianEntityMap.get(name).getNationality(), "中国")) {
                            academicianEntityMap.get(name).setCompanyType("镜外单位");
                        } else {
                            academicianEntityMap.get(name).setCompanyType(getCompanyType(data));
                        }
                    } else {
//                        academicianEntityMap.put(name, academicianEntity.builder().name(name).workCompany(data).companyType(getCompanyType(data)).build());
                    }
                }
                if (StringUtils.equalsAny(key, "所属学部")) {
                    data = trElements.get(finalJ).select("td").get(value).text().replaceAll(" ", "");
                    if (academicianEntityMap.containsKey(name)) {
                        academicianEntityMap.get(name).setDepartment(data);
                    } else {
//                        academicianEntityMap.put(name, academicianEntity.builder().name(name).department(data).build());
                    }
                }
                // 院士头衔
                if (type != null && type != -1) {
                    if (academicianEntityMap.containsKey(name)) {
                        academicianEntityMap.get(name).setAcademicianTitle(type);
                        academicianEntityMap.get(name).setAcademicianTitleValue(AcademicianEnum.getEnum(type).getValue());
                    } else {
//                        academicianEntityMap.put(name, academicianEntity.builder().name(name).academicianTitle(type).build());
                    }
                }
            }
        }
        return academicianEntityMap;
    }

    public static Map<String, AcademicianEntity> setNewMap(Map<String, AcademicianEntity> academicianEntityMap, Map<String, Integer> tbodyMap, Elements trElements, Integer type) {
        for (int j = 0; j < trElements.size(); j++) {
            //姓名
            int finalJ = j;
            String data = "";
            String name = "";
            AcademicianEntity academicianEntity = new AcademicianEntity();
            for (Map.Entry<String, Integer> entry : tbodyMap.entrySet()) {
                String key = entry.getKey();
                Integer value = entry.getValue();
                Elements tdElements = trElements.get(finalJ).select("td");
                if (!(tdElements.size() > 0) || tdElements.size() == 1) {
                    continue;
                }

                if (StringUtils.equalsAny(key, "姓名", "中文姓名")) {
                    name = tdElements.get(value).text().replaceAll(" ", "").replaceAll("（", "(").replaceAll("）", ")");

                    if (name.contains("女")) {
                        name = RegexUtils.replaceData(name, RegexUtils.ALL_KUA_REGEX, "");
                        name = CommonUtils.handleForeignName(name);
                        if (academicianEntityMap.containsKey(name)) {
                            academicianEntityMap.get(name).setSex("女");
                        } else {
                            name = RegexUtils.replaceData(name, RegexUtils.CHINA, "");
                            academicianEntityMap.put(name, academicianEntity.builder().name(name).sex("女").build());
                        }
                    } else {
                        name = RegexUtils.replaceData(name, RegexUtils.CHINA, "");
                        academicianEntityMap.put(name, academicianEntity.builder().name(name).sex("男").build());
                    }
                }
                if (StringUtils.equalsAny(key, "英文姓名")) {
                    data = trElements.get(finalJ).select("td").get(value).text().replaceAll(" ", "").replaceAll("\\?", "•");
                    if (academicianEntityMap.containsKey(name)) {
                        academicianEntityMap.get(name).setForeignName(data);
                    }
                }
                if (StringUtils.equalsAny(key, "出生年月", "出生日期")) {
                    data = trElements.get(finalJ).select("td").get(value).text().replaceAll(" ", "");
                    if (academicianEntityMap.containsKey(name)) {
                        academicianEntityMap.get(name).setBirthday(data);
                    } else {
                        academicianEntityMap.put(name, academicianEntity.builder().name(name).birthday(data).build());
                    }
                }
                if (StringUtils.equalsAny(key, "逝世日期")) {
                    data = trElements.get(finalJ).select("td").get(value).text().replaceAll(" ", "");
                    if (academicianEntityMap.containsKey(name)) {
                        academicianEntityMap.get(name).setDateOfDeath(data);
                    } else {
                        academicianEntityMap.put(name, academicianEntity.builder().name(name).dateOfDeath(data).build());
                    }
                }
                if (StringUtils.equalsAny(key, "生卒年月")) {
                    data = trElements.get(finalJ).select("td").get(value).text().replaceAll(" ", "").replaceAll("-", ".");
                    if (academicianEntityMap.containsKey(name)) {
                        academicianEntityMap.get(name).setBirthday(data.substring(0, 10));
                        academicianEntityMap.get(name).setDateOfDeath(data.substring(11));
                    } else {
                        academicianEntityMap.put(name, academicianEntity.builder().name(name).birthday(data.substring(0, 10)).dateOfDeath(data.substring(11)).build());
                    }
                }
                if (StringUtils.equalsAny(key, "专业", "学科领域", "学科专业")) {
                    data = trElements.get(finalJ).select("td").get(value).text().replaceAll(" ", "");
                    if (academicianEntityMap.containsKey(name)) {
                        academicianEntityMap.get(name).setMajor(data);
                    } else {
                        academicianEntityMap.put(name, academicianEntity.builder().name(name).major(data).build());
                    }
                }
                if (StringUtils.equalsAny(key, "工作单位", "所在单位")) {
                    if (academicianEntityMap.containsKey(name)) {
                        data = trElements.get(finalJ).select("td").get(value).text().replaceAll(" ", "");
                        academicianEntityMap.get(name).setWorkCompany(data);
                        academicianEntityMap.get(name).setCompanyType(getCompanyType(data));
                    } else {
                        academicianEntityMap.put(name, academicianEntity.builder().name(name).workCompany(data).companyType(getCompanyType(data)).build());
                    }
                }
                if (StringUtils.equalsAny(key, "国籍")) {
                    data = trElements.get(finalJ).select("td").get(value).text().replaceAll(" ", "");
                    if (academicianEntityMap.containsKey(name)) {
                        academicianEntityMap.get(name).setNationality(data);
                    } else {
                        academicianEntityMap.put(name, academicianEntity.builder().name(name).nationality(data).build());
                    }
                }
                if (StringUtils.equalsAny(key, "所属学部")) {
                    data = trElements.get(finalJ).select("td").get(value).text().replaceAll(" ", "");
                    if (academicianEntityMap.containsKey(name)) {
                        academicianEntityMap.get(name).setDepartment(data);
                    } else {
                        academicianEntityMap.put(name, academicianEntity.builder().name(name).department(data).build());
                    }
                }

                if (StringUtils.equalsAny(key, "获奖年份", "时间", "获奖时间", "年份")) {
                    data = trElements.get(finalJ).select("td").get(value).text().replaceAll(" ", "");
                    academicianEntityMap.put("tableOne", academicianEntity.builder().researchContents(data).build());
                }
                if (StringUtils.equalsAny(key, "获奖项目", "获奖项目名称", "项目名称", "成果名称", "奖项/荣誉", "荣誉称号", "荣誉表彰")) {
                    data = trElements.get(finalJ).select("td").get(value).text().replaceAll(" ", "");

                    if (academicianEntityMap.containsKey("tableTwo") && StringUtils.isNotBlank(academicianEntityMap.get("tableTwo").getAchievement())) {
                        academicianEntityMap.put("tableThree", academicianEntity.builder().honor(data).build());
                    } else {
                        academicianEntityMap.put("tableTwo", academicianEntity.builder().achievement(data).build());
                    }

                }
                if (StringUtils.equalsAny(key, "奖励名称", "获奖等级", "奖项", "奖项名称", "颁发单位", "授予单位")) {
                    data = trElements.get(finalJ).select("td").get(value).text().replaceAll(" ", "");
                    academicianEntityMap.put("tableThree", academicianEntity.builder().honor(data).build());
                }
                // 院士头衔
                if (type != null && type != -1) {
                    if (academicianEntityMap.containsKey(name)) {
                        academicianEntityMap.get(name).setAcademicianTitle(type);
                        academicianEntityMap.get(name).setAcademicianTitleValue(AcademicianEnum.getEnum(type).getValue());
                    } else {
                        academicianEntityMap.put(name, academicianEntity.builder().name(name).academicianTitle(type).build());
                    }
                }
            }
        }
//        if (MapUtils.isNotEmpty(academicianEntityMap) && academicianEntityMap.containsKey("tableOne")
//                && academicianEntityMap.containsKey("tableTwo")) {
//            JSONObject jsonObject = new JSONObject();
//            jsonObject.put("date", academicianEntityMap.get("tableOne").getResearchContents());
//            jsonObject.put("name", academicianEntityMap.get("tableTwo").getAchievement());
//            if (academicianEntityMap.containsKey("tableThree")) {
//                jsonObject.put("prizeCompany", academicianEntityMap.get("tableThree").getHonor());
//            }
//
//        }
        return academicianEntityMap;
    }

    /**
     * 获取table表固定字段名称的下标
     *
     * @param thead
     * @return
     */
    public static Map<String, Integer> getStringIntegerMap(Elements thead) {
        Map<String, Integer> tbodyMap = new HashMap<>();
        for (int j = 0; j < thead.size(); j++) {
            String text = CommonUtils.replaceBlank(thead.get(j).text()).replaceAll("　　", "");
            switch (text) {
                case "姓名":
                    tbodyMap.put(text, j);
                    break;
                case "中文姓名":
                    tbodyMap.put(text, j);
                    break;
                case "专业":
                    tbodyMap.put(text, j);
                    break;
                case "工作单位":
                    tbodyMap.put(text, j);
                    break;
                case "国籍":
                    tbodyMap.put(text, j);
                    break;
                case "英文姓名":
                    tbodyMap.put(text, j);
                    break;
                case "出生年月":
                    tbodyMap.put(text, j);
                    break;
                case "学科领域":
                    tbodyMap.put(text, j);
                    break;
                case "所在单位":
                    tbodyMap.put(text, j);
                    break;
                case "逝世日期":
                    tbodyMap.put(text, j);
                    break;
                case "生卒年月":
                    tbodyMap.put(text, j);
                    break;
                case "出生日期":
                    tbodyMap.put(text, j);
                    break;
                case "所属学部":
                    tbodyMap.put(text, j);
                    break;
                case "担任职务":
                    tbodyMap.put(text, j);
                    break;
                case "职务":
                    tbodyMap.put(text, j);
                    break;
                case "任职单位":
                    tbodyMap.put(text, j);
                    break;
                case "所获奖励及等级":
                    tbodyMap.put(text, j);
                    break;
                case "作者排名":
                    tbodyMap.put(text, j);
                    break;
                case "所获荣誉":
                    tbodyMap.put(text, j);
                    break;
            }
            if (StringUtils.equalsAny(text, "获奖年份", "时间", "获奖时间", "年份", "任职时间")) {
                tbodyMap.put(text, j);
            }
            if (StringUtils.equalsAny(text, "获奖项目", "获奖项目名称", "项目名称", "成果名称", "奖项/荣誉", "荣誉称号", "荣誉表彰", "荣誉", "荣誉/表彰")) {
                tbodyMap.put(text, j);
            }
            if (StringUtils.equalsAny(text, "奖励名称", "获奖等级", "奖项", "奖项名称", "颁发单位", "授予单位", "授予方")) {
                tbodyMap.put(text, j);
            }

        }
        return tbodyMap;
    }


    /**
     * 获取单位类型
     *
     * @param companyName 单位名称
     * @return
     */
    public static String getCompanyType(String companyName) {
        if (StringUtils.isBlank(companyName))
            return null;
        if (StringUtils.containsAny(companyName, "天文台", "研究所", "研究中心", "科学院", "研究院", "中心", "安全所", "总医院", "医院", "保健院",
                "总装备部", "农科所", "设计院", "长江三峡工程开发总公司", "中国航空武器实验训练靶场", "中国船舶重工集团公司第七研究院第715所", "研究总院", "大队", "总装备部")) {
            return CompanyEnum.GOVERNMENT_OFFICE.getValue();
        } else if (StringUtils.containsAny(companyName, "医学院", "研究生院", "学院", "大学", "分校", "大学分院")) {
            return CompanyEnum.SCHOOL.getValue();
        } else if (StringUtils.containsAny(companyName, "国家电网", "铁道部", "冶炼厂")) {
            return CompanyEnum.COUNTRY_COMPANY.getValue();
        } else if (StringUtils.containsAny(companyName, "境外单位", "外籍院士工作单位", "外籍")) {
            return CompanyEnum.FOREIGN_COMPANY.getValue();
        } else {
            return CompanyEnum.OTHER_COMPANY.getValue();
        }
    }

    /**
     * 新增2021年科学院院士
     *
     * @return
     * @throws IOException
     */
    public List<PersonInfo> addData() throws IOException {
        String url = "/htmlTest/2021-中国科学院增选院士名单.txt";
        Resource resource = new ClassPathResource(url);
        String filePath = resource.getFile().getAbsolutePath();
        File file = new File(filePath);
        List<String> lines = FileUtils.readLines(file, StandardCharsets.UTF_8);
        List<PersonInfo> list = new ArrayList<>();
        for (int i = 1; i < lines.size(); i++) {
            String[] data = lines.get(i).replaceAll("，", ",").split(",");
            PersonInfo personInfoTopy = new PersonInfo();
            for (int j = 0; j < data.length; j++) {
                personInfoTopy.setName(data[0]);
                personInfoTopy.setSex(data[1]);
                personInfoTopy.setMajor(data[3].replaceAll("（", "(").replaceAll("）", ")"));
                personInfoTopy.setWorkCompany(data[4].replaceAll("（", "(").replaceAll("）", ")"));
                personInfoTopy.setDepartment(data[5].replaceAll("（", "(").replaceAll("）", ")"));
            }
            personInfoTopy.setAcademicianTitle(0);
            personInfoTopy.setAcademicianTitleValue(AcademicianEnum.CAS_ACADEMICIAN.getValue());
            personInfoTopy.setElectionYear("2021");
            personInfoTopy.setTitleYears(0);

            list.add(personInfoTopy);
        }
        return list;
    }

    /**
     * 新增2021年科学院外籍院士
     *
     * @return
     * @throws IOException
     */
    public List<PersonInfo> addData1() throws IOException {
        String url = "/htmlTest/2021-中国科学院增选外籍院士.txt";
        Resource resource = new ClassPathResource(url);
        String filePath = resource.getFile().getAbsolutePath();
        File file = new File(filePath);
        List<String> lines = FileUtils.readLines(file, StandardCharsets.UTF_8);
        List<PersonInfo> list = new ArrayList<>();
        for (int i = 1; i < lines.size(); i++) {
            String[] split = lines.get(i).replaceAll("，", ",").split(",");
            PersonInfo personInfoTopy = new PersonInfo();
            for (int j = 0; j < split.length; j++) {
                personInfoTopy.setName(split[0]);
                personInfoTopy.setForeignName(split[1]);
                personInfoTopy.setNationality(split[2]);
                personInfoTopy.setMajor(split[3].replaceAll("（", "(").replaceAll("）", ")"));
                personInfoTopy.setWorkCompany(split[4].replaceAll("（", "(").replaceAll("）", ")"));
            }
            personInfoTopy.setAcademicianTitle(2);
            personInfoTopy.setAcademicianTitleValue(AcademicianEnum.CAS_FOREIGN_ACADEMICIAN.getValue());
            personInfoTopy.setElectionYear("2021");
            personInfoTopy.setTitleYears(0);
            list.add(personInfoTopy);
        }
        return list;
    }
}
